﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Permissions;
using System.Runtime.Serialization;

namespace MetaCollection.ContentService.Http
{
	/// <summary>
	/// Client Http Exception
	/// </summary>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors"), Serializable]
	public class ClientException : HttpException, ISerializable
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientException"/> class.
		/// </summary>
		public ClientException() : base() { Code = ClientErrorStatus.None; }
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientException"/> class.
		/// </summary>
		/// <param name="code">The code.</param>
		public ClientException(ClientErrorStatus code)
			: base((int)code, Enum.GetName(typeof(ClientErrorStatus), code))
		{ Code = code; }
		/// <summary>
		/// Initializes a new instance of the <see cref="HttpException&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="httpCode">The HTTP code.</param>
		/// <param name="message">The message.</param>
		public ClientException(ClientErrorStatus code, string message)
			: base((int)code, message)
		{ Code = code; }
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientException"/> class for a 401 Unauthorized error
		/// </summary>
		/// <param name="subCode">The sub code.</param>
		/// <param name="message">The message.</param>
		public ClientException(SubStatus401 subCode, string message)
			: base(401, message)
		{ Code = ClientErrorStatus.Unauthorized; SubCode = (int)subCode; }
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientException"/> class for a 403 Forbidden error
		/// </summary>
		/// <param name="subCode">The sub code.</param>
		/// <param name="message">The message.</param>
		public ClientException(SubStatus403 subCode, string message)
			: base(403, message)
		{ Code = ClientErrorStatus.Unauthorized; SubCode = (int)subCode; }
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientException"/> class for a 401 Unauthorized error
		/// </summary>
		/// <param name="subCode">The sub code.</param>
		/// <param name="message">The message.</param>
		public ClientException(SubStatus401 subCode, string message, Exception innerException)
			: base(401, message, innerException)
		{ Code = ClientErrorStatus.Unauthorized; SubCode = (int)subCode; }
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientException"/> class for a 403 Forbidden error
		/// </summary>
		/// <param name="subCode">The sub code.</param>
		/// <param name="message">The message.</param>
		public ClientException(SubStatus403 subCode, string message, Exception innerException)
			: base(403, message, innerException)
		{ Code = ClientErrorStatus.Unauthorized; SubCode = (int)subCode; }
		/// <summary>
		/// Initializes a new instance of the <see cref="HttpException&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="httpCode">The HTTP code.</param>
		/// <param name="message">The message.</param>
		/// <param name="innerException">The inner exception.</param>
		public ClientException(ClientErrorStatus code, string message, Exception innerException)
			: base((int)code, message, innerException)
		{ Code = code; }
		/// <summary>
		/// The HTTP Status Code
		/// </summary>
		public ClientErrorStatus Code { get; private set; }
		/// <summary>
		/// A sub status code (for 401 or 403 errors)
		/// </summary>
		public int SubCode { get; private set; }
		/// <summary>
		/// Gets information about the exception and adds it to the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> object.
		/// </summary>
		/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
		/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that holds the contextual information about the source or destination.</param>
		[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
		public new virtual void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
		}
	}
}
